package ar.com.angrymachine.hunter.utils;

import java.util.Vector;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.activities.BossAltarActivity;
import ar.com.angrymachine.hunter.activities.ExploreActivity;
import ar.com.angrymachine.hunter.gameplay.Database;
import ar.com.angrymachine.hunter.gameplay.Dungeon;
import ar.com.angrymachine.hunter.gameplay.Quest;
import ar.com.angrymachine.hunter.gameplay.Room;
import ar.com.angrymachine.utils.Util;

public class PortalDungeonGenerator implements DungeonGenerator
{
	//TODO: implement properly
	private Dungeon currentDungeon;
	private Vector deadEndNodes = new Vector(); // all the nodes with no further exploration discoveries
	
	public Dungeon generate(Quest quest)
	{
    	// spawn a random generated branching dungeon
    	currentDungeon = new Dungeon(quest);
    	createDungeonNetwork(currentDungeon, 6);
    	// to add the portal, choose the last exploration node
    	ExploreActivity portalNode = (ExploreActivity) deadEndNodes.lastElement();
    	portalNode.addDiscovery(new BossAltarActivity(5000), portalNode.maxExploration);
    	return currentDungeon;
	}
	
    private Dungeon createDungeonNetwork(Dungeon dungeon, int rooms)
    {
    	// create the main hub room
    	Room hubRoom = (Room) Database.getRandomLeveledEntity(0, Database.rooms);
    	ExploreActivity hub = new ExploreActivity(hubRoom, 13000);
    	// spawn all the available rooms
        int[] times = DungeonGenerationUtils.createEventTimeline(rooms, hub.maxExploration);
        for (int i = 0; i < times.length; i++)
        {
        	// create a sensible room type here
        	ExploreActivity newRoom = DungeonGenerationUtils.createRoom(currentDungeon, i+1);
        	Activity finalActivity = DungeonGenerationUtils.lockBehindDoor(currentDungeon, newRoom, 33);
        	hub.addDiscovery(finalActivity, times[i]);
            if(i >= times.length/2)
            {
            	deadEndNodes.add(newRoom);
            }
        }
        // add some random stuff
        DungeonGenerationUtils.populateLocation(currentDungeon, hub, rooms/2, Util.getRandom(hub.maxExploration/10000, (hub.maxExploration/10000)*2));
        dungeon.addActivity(hub);
        return dungeon;    	
    }    
}
